package br.com.quad.dao;

import java.util.List;

import br.com.quad.bean.Servico;
import br.com.quad.bean.TipoServico;
import br.com.quad.bean.Veiculo;

public class ServicoDAOHibernate implements ServicoDAO {
	
	HibernateDAOFactory hibernateDAOFactory = null;
	HibernateDAO<Servico> hibernateDAO = null;
	
	
	public ServicoDAOHibernate(){
		hibernateDAOFactory = new HibernateDAOFactory();
		hibernateDAO = hibernateDAOFactory.getServicoDAO();
	}


	@Override
	public Servico inserir(Servico servico) throws Exception {

		try {
			servico = (Servico) hibernateDAO.add(servico);			
			
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 
		return servico;
	}


	@Override
	public boolean alterar(Servico servico) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.update(servico);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}

		return ok;
	}

	@Override
	public boolean excluir(Servico servico) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.remove(servico);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}

		return ok;
	}

	@Override
	public boolean isExclusaoLiberada(Servico servico) {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Servico> getSelecionar(String descricaoPesquisa,String nomePesquisa,String cPF_CNPJPesquisa, int codigoTipoServicoPesquisa){
		String 	sql = 	"	SELECT distinct(s)" +
						"	FROM Servico as s  " +
						"	INNER JOIN s.tipoServicos" +
						"	WHERE s.codigoServico <> 0";


		if (!(descricaoPesquisa.isEmpty())) {
			sql += " AND s.descricao like '" + descricaoPesquisa + "%' ";  
		}	
		
		if (!(nomePesquisa.isEmpty())) {
			sql += " AND s.cliente.nome like '" + nomePesquisa + "%' ";  
		}
		
		if (!(cPF_CNPJPesquisa.isEmpty())) {
			sql += " AND s.cliente.CFP_CNPJ like '" + cPF_CNPJPesquisa + "%' ";  
		}
		
		if ((codigoTipoServicoPesquisa != 0)) {
			sql += " AND s.TipoServico.codigoTipoServico = " + codigoTipoServicoPesquisa + " ";  
		}

		sql += " ORDER BY s.descricao";		
 
		List<Servico> lista = hibernateDAO.getSession().createQuery(sql).list();

		return lista;
	}


	@Override
	public Servico getServico(int codigoServico) {
		String 	sql = "FROM Servico as s WHERE s.codigoServico = "+codigoServico;
		return (Servico) hibernateDAO.getSession().createQuery(sql).uniqueResult();
	}

	@Override
	public List<Servico> getListarTodos() {
		
		return hibernateDAO.listAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExiste(Servico servico){
		String 	sql = 	" FROM Servico " +
						" WHERE codigoServico <> "+servico.getCodigoServico()+""+
						" AND descricao like '" + servico.getDescricao() + "%' ";

		List<TipoServico> lista = hibernateDAO.getSession().createQuery(sql).list();
	    
		if (lista.size()<1) {
			return false;
		} else {
			return true;
		}
	}
	
	@Override
	public void inicialize(Servico servico){
		
		Servico s = DAOFactory.getServicoDAO().getServico(servico.getCodigoServico());
		s.getTipoServicos();
		servico.setTipoServicos(s.getTipoServicos());
		
		
	}

}
